﻿Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Class AddBook
    Inherits System.Web.UI.Page
    'Variable to store the id of this beach.
    Public Shared bookIDFinal As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.Browser.IsMobileDevice Then
            Response.Redirect("~/Add_Book_mobile.aspx")
        End If
    End Sub

    Protected Sub btn_AddBook_Click(sender As Object, e As EventArgs) Handles btn_AddBook.Click

        'Create a unique filename
        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        Dim ext As String = IO.Path.GetExtension(f_Picture.FileName)
        Dim newFileName As String = myGUID.ToString() + ext
        'Save picture to disk
        If (f_Picture.HasFile And isPicture(ext)) Then
            Dim picLocationOnHardDiskServer = Request.MapPath("Pictures") & "/" & newFileName
            f_Picture.SaveAs(picLocationOnHardDiskServer)

            'resizes an image to 100x100
            Dim img1 As System.Drawing.Image = System.Drawing.Image.FromFile(MapPath("~/Pictures/") + newFileName)
            Dim bmp1 As System.Drawing.Image = img1.GetThumbnailImage(100, 100, Nothing, IntPtr.Zero)
            bmp1.Save(MapPath("~/Thumbnails/") + newFileName)
            'resizes image to 500x400 for front page banner
            Dim img2 As System.Drawing.Image = System.Drawing.Image.FromFile(MapPath("~/Pictures/") + newFileName)
            Dim bmp2 As System.Drawing.Image = img2.GetThumbnailImage(400, 500, Nothing, IntPtr.Zero)
            bmp2.Save(MapPath("~/Thumbnails/Banner/") + newFileName)
        Else
            Response.Redirect("AddBook.aspx")
        End If

        If (ddl_Genre.SelectedValue > 0 AndAlso ddl_Language.SelectedValue > 0) Then
            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
            Dim cmd As New SqlCommand("INSERT INTO Books(BookName, SubCategory_FK, Description, Review, Picture, Thumbnail, Bibliophile, CodingLanguage_FK, BannerPicture) VALUES (@f1, @f2, @f3, @f4, @f5, @F6, @f7, @f8, @f9)", conn)

            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", tb_bookName.Text)
            cmd.Parameters.AddWithValue("@f2", ddl_SubCategory.SelectedValue)
            cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
            cmd.Parameters.AddWithValue("@f4", tb_Review.Text)
            cmd.Parameters.AddWithValue("@f5", "Pictures/" & newFileName)
            cmd.Parameters.AddWithValue("@f6", "Thumbnails/" & newFileName)
            cmd.Parameters.AddWithValue("@f7", User.Identity.Name)
            cmd.Parameters.AddWithValue("@f8", ddl_Language.SelectedValue)
            cmd.Parameters.AddWithValue("@f9", "Thumbnails/Banner/" & newFileName)
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

            'Find id of the new book.
            Dim getLastIdCmd As SqlCommand = New SqlCommand("SELECT TOP 1 ID FROM Books ORDER BY ID DESC", conn)
            conn.Open()
            Dim dr = getLastIdCmd.ExecuteReader()
            dr.Read()
            bookIDFinal = dr.GetValue(0)
            conn.Close()

            Dim cmdRating As New SqlCommand("INSERT INTO BookUserRating(UserRating, Bibliophile, Book_FK) VALUES (@f1, @f2, @f3)", conn)
            cmdRating.CommandType = CommandType.Text
            cmdRating.Parameters.AddWithValue("@f1", rbl_Rating.SelectedValue)
            cmdRating.Parameters.AddWithValue("@f2", User.Identity.Name)
            cmdRating.Parameters.AddWithValue("@f3", bookIDFinal)
            conn.Open()
            cmdRating.ExecuteNonQuery()
            conn.Close()
            Response.Redirect("Default.aspx")
        Else
            Response.Redirect("AddBook.aspx")
        End If

    End Sub

    'Check if picture uploaded is a valid picture.
    Private Function isPicture(ByVal pictureExtension As String) As Boolean
        Select Case pictureExtension
            Case ".jpg"
                Return True
            Case ".JPG"
                Return True
            Case ".jpeg"
                Return True
            Case ".JPEG"
                Return True
            Case ".png"
                Return True
            Case ".PNG"
                Return True
            Case ".bmp"
                Return True
            Case ".BMP"
                Return True
            Case ".gif"
                Return True
            Case ".GIF"
                Return True
            Case Else
                Return False
        End Select
    End Function
End Class